using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Gospel.DB
{
	/// <summary>
	/// Strongly-typed collection for the User class.
	/// </summary>
	[Serializable]
	public partial class UserCollection : ActiveList<User, UserCollection> 
	{	   
		public UserCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the vbsa_users table.
	/// </summary>
	[Serializable]
	public partial class User : ActiveRecord<User>
	{
		#region .ctors and Default Settings
		
		public User()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public User(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public User(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public User(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("vbsa_users", TableType.Table, DataService.GetInstance("VBSA"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarPersonId = new TableSchema.TableColumn(schema);
				colvarPersonId.ColumnName = "person_id";
				colvarPersonId.DataType = DbType.Int32;
				colvarPersonId.MaxLength = 0;
				colvarPersonId.AutoIncrement = true;
				colvarPersonId.IsNullable = false;
				colvarPersonId.IsPrimaryKey = true;
				colvarPersonId.IsForeignKey = false;
				colvarPersonId.IsReadOnly = false;
				colvarPersonId.DefaultSetting = @"";
				colvarPersonId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPersonId);
				
				TableSchema.TableColumn colvarLoginName = new TableSchema.TableColumn(schema);
				colvarLoginName.ColumnName = "login_name";
				colvarLoginName.DataType = DbType.String;
				colvarLoginName.MaxLength = 30;
				colvarLoginName.AutoIncrement = false;
				colvarLoginName.IsNullable = false;
				colvarLoginName.IsPrimaryKey = false;
				colvarLoginName.IsForeignKey = false;
				colvarLoginName.IsReadOnly = false;
				colvarLoginName.DefaultSetting = @"";
				colvarLoginName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLoginName);
				
				TableSchema.TableColumn colvarPassword = new TableSchema.TableColumn(schema);
				colvarPassword.ColumnName = "password";
				colvarPassword.DataType = DbType.String;
				colvarPassword.MaxLength = 30;
				colvarPassword.AutoIncrement = false;
				colvarPassword.IsNullable = false;
				colvarPassword.IsPrimaryKey = false;
				colvarPassword.IsForeignKey = false;
				colvarPassword.IsReadOnly = false;
				colvarPassword.DefaultSetting = @"";
				colvarPassword.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPassword);
				
				TableSchema.TableColumn colvarActive = new TableSchema.TableColumn(schema);
				colvarActive.ColumnName = "active";
				colvarActive.DataType = DbType.Boolean;
				colvarActive.MaxLength = 0;
				colvarActive.AutoIncrement = false;
				colvarActive.IsNullable = false;
				colvarActive.IsPrimaryKey = false;
				colvarActive.IsForeignKey = false;
				colvarActive.IsReadOnly = false;
				
						colvarActive.DefaultSetting = @"((1))";
				colvarActive.ForeignKeyTableName = "";
				schema.Columns.Add(colvarActive);
				
				TableSchema.TableColumn colvarCreated = new TableSchema.TableColumn(schema);
				colvarCreated.ColumnName = "created";
				colvarCreated.DataType = DbType.DateTime;
				colvarCreated.MaxLength = 0;
				colvarCreated.AutoIncrement = false;
				colvarCreated.IsNullable = false;
				colvarCreated.IsPrimaryKey = false;
				colvarCreated.IsForeignKey = false;
				colvarCreated.IsReadOnly = false;
				
						colvarCreated.DefaultSetting = @"(getdate())";
				colvarCreated.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreated);
				
				TableSchema.TableColumn colvarRoleId = new TableSchema.TableColumn(schema);
				colvarRoleId.ColumnName = "role_id";
				colvarRoleId.DataType = DbType.Byte;
				colvarRoleId.MaxLength = 0;
				colvarRoleId.AutoIncrement = false;
				colvarRoleId.IsNullable = false;
				colvarRoleId.IsPrimaryKey = false;
				colvarRoleId.IsForeignKey = false;
				colvarRoleId.IsReadOnly = false;
				
						colvarRoleId.DefaultSetting = @"((1))";
				colvarRoleId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRoleId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["VBSA"].AddSchema("vbsa_users",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("PersonId")]
		public int PersonId 
		{
			get { return GetColumnValue<int>("person_id"); }

			set { SetColumnValue("person_id", value); }

		}

		  
		[XmlAttribute("LoginName")]
		public string LoginName 
		{
			get { return GetColumnValue<string>("login_name"); }

			set { SetColumnValue("login_name", value); }

		}

		  
		[XmlAttribute("Password")]
		public string Password 
		{
			get { return GetColumnValue<string>("password"); }

			set { SetColumnValue("password", value); }

		}

		  
		[XmlAttribute("Active")]
		public bool Active 
		{
			get { return GetColumnValue<bool>("active"); }

			set { SetColumnValue("active", value); }

		}

		  
		[XmlAttribute("Created")]
		public DateTime Created 
		{
			get { return GetColumnValue<DateTime>("created"); }

			set { SetColumnValue("created", value); }

		}

		  
		[XmlAttribute("RoleId")]
		public byte RoleId 
		{
			get { return GetColumnValue<byte>("role_id"); }

			set { SetColumnValue("role_id", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Gospel.DB.ArticleCollection Articles()
		{
			return new Gospel.DB.ArticleCollection().Where(Article.Columns.PersonId, PersonId).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varLoginName,string varPassword,bool varActive,DateTime varCreated,byte varRoleId)
		{
			User item = new User();
			
			item.LoginName = varLoginName;
			
			item.Password = varPassword;
			
			item.Active = varActive;
			
			item.Created = varCreated;
			
			item.RoleId = varRoleId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varPersonId,string varLoginName,string varPassword,bool varActive,DateTime varCreated,byte varRoleId)
		{
			User item = new User();
			
				item.PersonId = varPersonId;
				
				item.LoginName = varLoginName;
				
				item.Password = varPassword;
				
				item.Active = varActive;
				
				item.Created = varCreated;
				
				item.RoleId = varRoleId;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string PersonId = @"person_id";
			 public static string LoginName = @"login_name";
			 public static string Password = @"password";
			 public static string Active = @"active";
			 public static string Created = @"created";
			 public static string RoleId = @"role_id";
						
		}

		#endregion
	}

}

